package cn.mall.user.controller;

import cn.mall.user.dto.CarDto;
import cn.mall.user.service.ICarService;
import cn.mall.user.domain.Car;
import cn.mall.user.query.CarQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car" )
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Car car) {
        try {
            if ( car.getId() != null)
                if(car.getGid()!=null && car.getSid()!=null&&car.getUid()!=null&&car.getCount()!=null&&!car.getUrl().equals("")){
                    carService.update(car);
                }else
                    return AjaxResult.me().setSuccess(false).setMessage("请完整填写信息");

            else
            if(car.getGid()!=null && car.getSid()!=null&&car.getUid()!=null&&car.getCount()!=null&&!car.getUrl().equals("")){
                carService.insert(car);
            }else
                return AjaxResult.me().setSuccess(false).setMessage("请完整填写信息");
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    public AjaxResult remove(@PathVariable("id" ) Long id) {
        try {
            carService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param
     * @return
    */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody CarDto carDto) {
        try {
            carService.patchRemove(carDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
    * 根据Id获取用户
    * @param id
    * @return
    */
    @GetMapping("/{id}" )
    public AjaxResult loadById(@PathVariable("id" ) Long id) {
        try {
            Car car =carService.loadById(id);
            return AjaxResult.me().setResultObj(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult loadAll() {

        try {
            List< Car> list = carService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult pageList(@RequestBody CarQuery query) {
        try {
            if(query.getUid()!=null){
                PageList<Car> pageList = carService.pageList(query);
                return AjaxResult.me().setResultObj(pageList);
            }else
                return AjaxResult.me().setSuccess(false).setMessage("用户id未上传，无法查询");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }
}
